<?php

namespace Admin\Controller;

class UserController extends BaseController {

    /**
     * 加载登陆页面
     */
    public function login() {

        if (IS_GET) {
            $this->display();
        } else {
            // echo 200;die;
            $data = I('post.');
            $data['password'] = md5($data['password']);
            $user = D('user');
            if (empty($data)) {
                return false;
            }
            $data['status'] = 0;
            $result = $user->where($data)->select();
            if ($result) {
                //写入session
                session('user', $result[0]);
                $data['login_time'] = date('Y-m-d H:i:s', time());

                $res = $user->where('id=' . $result[0]['id'])->save($data);
                if ($res) {
                    $this->ajaxReturn(200);
                } else {
                    $this->ajaxReturn(400);
                }
                $this->ajaxReturn(200);
            } else {
                $this->ajaxReturn(400);
            }
        }
    }

    /**
     * 添加管理员
     */
    public function add() {

        $user = D('user');
        if (IS_GET) {
            // var_dump(session('user'));die;
            $this->display();
        } else {

            $data = I('post.');
            // 文件上传返回路径
            // $image = $user -> image();
            // var_dump($data);die;

            $data['password'] = md5($data['password']);
            $data['repassword'] = md5($data['repassword']);
            // $data['image'] = $image;
            $data['add_time'] = date('Y-m-d H:i:s', time());
            // var_dump($data);die;
            if ($user->create($data) && $user->add()) {
                $this->success('成功');
            } else {
                $this->error($user->getError());
            }
        }
    }

    public function uploadImg() {
        $user = D('user');
        if (IS_AJAX) {
            $image = $user->image();
            $this->ajaxReturn($image);
        }
    }

    /**
     * 管理员列表
     */
    public function lists() {
        $user = D('user');
        if (IS_POST) {
             $res = I('post.');
                switch ($res['seek_type']) {
                    case 'nikename':
                        $where['nikename'] = ['like','%'.$res['keyword'].'%'];
                        break;
                    case 'username':
                        $where['username'] = ['like','%'.$res['keyword'].'%'];
                        break;
                    case 'phone':
                        $where['phone'] = ['like','%'.$res['keyword'].'%'];
                        break;
                    case 'email':
                        $where['email'] = ['like','%'.$res['keyword'].'%'];
                        break;             
                    default:
                        $where['nikename'] = ['like','%'.$res['keyword'].'%'];
                        break;
            }
        }
        // var_dump($res['seek_type']);
        $where['status'] = 0;
        $data = $user->where($where)->select();
        
        if ($data) {
            $this->assign('data', $data);
            $this->assign('res', $res);
            $this->display();
        } else {
            $this->display();
        }
    }
    //搜索条件
    public function seekType(){
       

        var_dump($where);die;
    }
    /**
     * 删除管理员
     */
    public function destroy() {
        $id = I('get.id');
        $where['id'] = $id;
        $data['delete_time'] = date('Y-m-d H:i:s', time());
        $data['status'] = 1;
        $user = D('user');
        if (!empty($id)) {
            $user->where($where)->save($data);
            echo 200;
        } else {
            echo 404;
        }
    }

    //修改管理员
    public function edit() {
        $id = I('get.id');
        $user = D('user');
        if (IS_GET) {
            $result = $user->where('id=' . $id)->select();
            if ($result) {
                $this->assign('result', $result[0]);
                $this->display();
            }
        } else {

            $data = I('post.');

            $user->where('id=' . $data['id'])->create();
            $data = $user->save();
            if ($data) {
                $this->success('成功');
                // $this -> redirect(U('Admin/User/lists'));
            } else {
                $this->error($user->getError());
            }

            // var_dump($data);die;
        }

        // var_dump($result);die;
    }

    //退出登录
    public function exits() {
        //清除session
        session('user', null);
        $this->redirect(U('Admin/User/login'));
    }

}
